/**
 * Source file for the {@link fr.prolavage.domain.beans.CouplageTypeVehiculeFactice} type.
 * 
 * This source file belongs to a software project managed by the
 * Prolavage SARL company located in FRANCE.
 * Prolavage operates a truck washing station.
 *
 * This software project helps Prolavage manages its enterprise resources.
 * The main features consists in printing a ticket for each customer
 * with the details of the vehicle washing service to be provided.
 *
 * Copyright (C) 2014 Prolavage SARL
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * This software project MUST comply with the law of FRANCE, and with
 * the law and regulations of the European Union.
 * Since this software project is provided without any warranty,
 * it MAY NOT comply with the law of any other country.
 *
 * This source file MAY contain text written in the english and/or french languages.
 * For legal considerations, the french language SHOULD take precedence.
 *
 * This software project is hosted on https://code.google.com/p/prolavage/
 * Do not hesitate to visit our blog as well: http://prolavage.fr
 *
 * We can be contacted by postmail using the following address:
 *
 * Pro Lavage Poids Lourds SARL
 * ZI du Cormier – Bd du Cormier
 * 49300 CHOLET
 * FRANCE
 *
 */

package fr.prolavage.domain.beans;

import static org.junit.Assert.assertNotNull;

import org.apache.commons.lang3.builder.EqualsBuilder;

import com.google.common.collect.ImmutableSet;

/**
 * Instance factice pour les jeux de tests utilisant la classe {@link CouplageTypeVehicule}.
 * 
 * @author Adrien COLOM
 * 
 */
public abstract class CouplageTypeVehiculeFactice extends CouplageTypeVehicule {

   private static final long serialVersionUID = -1071303552010910009L;

   static void linkOneToOne(final SuperTypeVehicule superType, final CouplageTypeVehicule couplage,
         final SousTypeVehicule sousType) {

      assertNotNull(couplage);

      couplage.setSousType(sousType);
      couplage.setSuperType(superType);
   }

   protected void clear() {

      setDesignation(null);
      setId(null);
      setSousType(null);
      setSuperType(null);
   }

   /**
    * @see fr.prolavage.domain.beans.TypeVehicule#getDesignation()
    */
   @Override
   public String getDesignation() {

      initIfRequired();
      return super.getDesignation();
   }

   /**
    * @see fr.prolavage.domain.beans.Entite#getId()
    */
   @Override
   public Integer getId() {

      initIfRequired();
      return super.getId();
   }

   /**
    * @see fr.prolavage.domain.beans.CouplageTypeVehicule#getSousType()
    */
   @Override
   public SousTypeVehicule getSousType() {

      initIfRequired();
      return super.getSousType();
   }

   /**
    * @see fr.prolavage.domain.beans.CouplageTypeVehicule#getSuperType()
    */
   @Override
   public SuperTypeVehicule getSuperType() {

      initIfRequired();
      return super.getSuperType();
   }

   protected abstract void init();

   void initIfRequired() {

      if (!isInitialized()) {
         init();
      }
   }

   /**
    * Getter.
    * 
    * @return Indicates if the instance holds the test data set.
    */
   public synchronized boolean isInitialized() {

      final EqualsBuilder equalsBuilder = new EqualsBuilder();
      equalsBuilder.append(true, (null != super.getSousType()));
      equalsBuilder.append(true, (null != super.getSuperType()));
      return equalsBuilder.build();
   }

   /**
    * Setter.
    * 
    * @param initialized
    *           Indicates if the instance holds the test data set.
    */
   public synchronized void setInitialized(final boolean initialized) {

      if (!initialized) {
         clear();
      } else {
         initIfRequired();
      }
   }

   /**
    * @see fr.prolavage.domain.beans.CouplageTypeVehicule#setSousType(fr.prolavage.domain.beans.SousTypeVehicule)
    */
   @Override
   public void setSousType(final SousTypeVehicule sousType) {

      super.setSousType(sousType);

      if (null != sousType) {
         sousType.setCouplages(ImmutableSet.<CouplageTypeVehicule> of(this));
      }
   }

   /**
    * @see fr.prolavage.domain.beans.CouplageTypeVehicule#setSuperType(fr.prolavage.domain.beans.SuperTypeVehicule)
    */
   @Override
   public void setSuperType(final SuperTypeVehicule superType) {

      super.setSuperType(superType);

      if (null != superType) {
         superType.setCouplages(ImmutableSet.<CouplageTypeVehicule> of(this));
      }
   }

   /**
    * @see fr.prolavage.domain.beans.Entite#toString()
    */
   @Override
   public String toString() {

      initIfRequired();
      return super.toString();
   }
}
